angular.module('controllers',['ngPlugin'])
    .controller('authorizeController',['$scope','$timeout','tinymceConfig','notify','$http','$state','$stateParams','ngModal',function($scope,$timeout,tinymceConfig,notify,$http,$state,$stateParams,ngModal){
        $scope.isAuthorize = -1;
        $scope.authorize = {};
        $http({
            method:'get',
            url: '/web-api/configurations/wechat',
            headers: JSON.parse($('meta[name="profile"]').attr('content')),
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
        }).success(function (data) {
            if (data.code == 200) {
               if(data.data && data.data['app_id']&&data.data['app_secret']&&data.data['group_code']){
                   $scope.isAuthorize = 1;
                   $scope.authorize = data.data;
               }else{
                   $scope.isAuthorize = 0;
               }
            } else {
                notify(data.message, 3);
                $scope.isAuthorize = 0;
            }
        }).error(function (data) {
            notify(data.message, 3);
        });
        $scope.addSubscription = function($event){
            ngModal({
                templateUrl: '/components/addSubscription.html',
                controller: 'subscriptionModalController',
                resolve: {
                    items: function () {
                        return {
                            isAuthorize: $scope.isAuthorize,
                            authorize: $scope.authorize
                        };
                    }
                },
                ok: function (authorize) {

                },
                cancel: function () {

                }
            });
        }
    }])
    .controller('subscriptionModalController',['$scope','$timeout','$http','$modalInstance','notify','items',function ($scope,$timeout,$http,$modalInstance,notify,items) {
        var profile = JSON.parse($('meta[name="profile"]').attr('content'));
        if(items.isAuthorize==1){
            $scope.postData = items.authorize;
        }else{
            $scope.postData = {
                app_id: '',
                app_secret: '',
                group_code: profile.group_code,
            }
        }
        $scope.ok = function ($event) {
            if(!$scope.postData.app_id){
                notify('APP_ID不能为空', 3);
                return;
            }
            if(!$scope.postData.app_secret){
                notify('SecurityKey不能为空', 3);
                return;
            }
            var $btn = angular.element($event.target).addClass('disabled');
            $http({
                method:items.isAuthorize==1?'PATCH':'POST',
                url: '/web-api/configurations/wechat',
                headers: JSON.parse($('meta[name="profile"]').attr('content')),
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: $scope.postData,
            }).success(function (data) {
                $btn.removeClass('disabled');
                if (data.code == 200) {
                    if(items.isAuthorize==0){
                        notify('授权成功', 2);
                    }else{
                        notify('修改成功', 2);
                    }
                    $modalInstance.close($scope.postData);
                } else {
                    notify(data.message, 3);
                }
            }).error(function (data) {
                $btn.removeClass('disabled');
                notify(data.message, 3);
            });
        };
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);
